<style scoped>
.title{
    margin-bottom:16px;
}
</style>
<template>
    <div class="delivery-list-page">
        <p class="title">
            <el-button class="bolder outsideBtnBS" size="mini" type="danger" :disabled="isClick" @click="saveNotice()">保存</el-button>
            <el-button class="bolder outsideBtnBS" size="mini" type="danger" :disabled="isClick" @click="commitNotice()">提交</el-button>
            <el-button class="bolder outsideBtnBS" size="mini" type="danger">打印</el-button>
        </p>
        <el-form :inline="true" :model="formInline" class="demo-form-inline" style="margin-bottom:20px;border-bottom:1px solid #ccc;">
            <el-form-item label="预入库单号" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.shipNoticeNo" disabled></el-input>
            </el-form-item>
            <el-form-item label="制单日期" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.createTime" disabled></el-input>
            </el-form-item>
            <el-form-item label="制单人" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.shipNoticceUserName" disabled></el-input>
            </el-form-item>
            <!--<el-form-item label="供应商编码" class="selectMB10">
                <el-input  v-model="formInline.partnerCode" disabled></el-input>
            </el-form-item>
            <el-form-item label="供应商名称" class="selectMB10">
                <el-input  v-model="formInline.partnerName" disabled></el-input>
            </el-form-item>-->
            <el-form-item label="收货地(简称)" class="selectMB10" :label-width="labelWidth100">
                <el-input  v-model="formInline.shortName" disabled></el-input>
            </el-form-item>
            <el-form-item label="详细地址" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.adressDetail" disabled></el-input>
            </el-form-item>
            <!--<el-form-item label="库房地址" class="selectMB10">
                <el-input  v-model="formInline.adressDetailKf" disabled></el-input>
            </el-form-item>-->
            <el-form-item label="联系人" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.linkMan" disabled></el-input>
            </el-form-item>
            <el-form-item label="联系电话" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.linkManMobile" disabled></el-input>
            </el-form-item>
            <el-form-item label="备注" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.shipNoticeBak" @change="isClick=false;"></el-input>
            </el-form-item>
        </el-form>
        <el-table :data="createDeliveryData" border style="margin-top:16px;width: 100%;" v-loading.body="loading">
            <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>

            <el-table-column
                    prop="poCode"
                    header-align="center"
                    label="PO"
                    width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="currHopeTime"
                    header-align="center"
                    label="期望到货时间"
                    width="120" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="materialCode"
                    header-align="center"
                    label="备件编码"
                    width="110" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="materialName"
                    header-align="center"
                    label="备件名称"
                    width="110" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="standard"
                    header-align="center"
                    label="规格"
                    width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="materialUnit"
                    header-align="center"
                    label="单位"
                    width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="shipReqQuantity"
                    header-align="center"
                    label="发货请求数量"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="notCurrQuantity"
                    header-align="center"
                    label="未发数量"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="sendQuantity"
                    header-align="center"
                    label="本次发货数量"
                    width="130">
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.sendQuantity" placeholder="请输入本次发货数量"  @change="inputCurrQuantity(scope.$index, scope.row)"></el-input>
                </template>
            </el-table-column>
            <el-table-column
                    prop="noticeUnit"
                    header-align="center"
                    label="件（箱）数"
                    width="130">
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.noticeUnit" placeholder="请输入本次件（箱）数"  @change="inputSpeQuantity(scope.$index, scope.row)"></el-input>
                </template>
            </el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination v-if="isPaging" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"  :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage" :total="totalNumber"></el-pagination>

    </div>
</template>
<script>
    export default {
        data(){
            return{
                labelWidth:"80px",
                labelWidth100:"100px",
                // 查询表单
                formInline:{
                    shipNoticeNo:"",
                    poCode:"",
                    createTime:"",
                    shipNoticceUserName:"",
                    adressDetail:"",
                    linkMan:"",
                    linkManMobile:"",
                    shipNoticeBak:"",
                    shipRequestStoreNo:"",//库房编号
                    shipAdressNo:""//发货地址编号
                },
                createDeliveryData:[],//送货明细列表
                paramDataList:[],//接收参数，进行传参
                isNum:false,//添加验证送货单位是否是数字
                isCurrNum:false,//验证本次送货数量是否时数字
                isTure:false,//本次发货数量必须小于未发数量
                isClick:false,//禁止连点
                loading:false,

                // 分页
                pageIndex:1,    //当前页（必传）
                pageSize :10,   //每页多少条
                totalNumber: 20,
                totalPage:2,
                isPaging:false
            }
        },
        //进入页面立即运行此方法
        mounted: function () {
            var _this = this;
            _this.$store.commit('currentActive', 'CreateDeliverySheet');
            //console.log(_this.$route.query.paramDataList)
            _this.paramDataList=_this.$route.query.paramDataList;//传参
            _this.getDeliveryInfor();
        },
        methods: {
            //点击创建送货单，一进入页面就获取
            getDeliveryInfor(){
                var _this=this;
                _this.loading=true;
                var tmp={
                    paramDataList:_this.paramDataList,
                    pageIndex:_this.pageIndex,
                    pageSize:_this.pageSize
                }
                //console.log(JSON.stringify(tmp))

                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"shipNotice/toCreateNoticeInfo",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        //console.log(data)
                        if (data.success) {
                            _this.loading=false;
                            _this.isPaging=true;
                            _this.totalPage=data.data.totalPage;
                            _this.totalNumber=data.data.totalNumber;
                            _this.createDeliveryData=data.data.dataList;//送货明细表
                            _this.formInline.shipNoticeNo=data.data.shipNoticeNo;//预入库单号
                            _this.formInline.createTime=data.data.createTime;//制单日期
                            _this.formInline.shipNoticceUserName=data.data.shipNoticceUserName;//制单人
                            /*_this.formInline.partnerCode=data.partnerCode;//供应商编码         划掉
                            _this.formInline.partnerName=data.partnerName;//供应商名称          划掉*/
                            _this.formInline.adressDetail=data.data.adressDetail;//送货地
                            _this.formInline.shortName=data.data.shortName;//送货地
                            _this.formInline.shipRequestStoreNo=data.data.shipRequestStoreNo;
                            _this.formInline.shipAdressNo=data.data.shipAdressNo;
                            /*_this.formInline.adressDetailKf=data.adressDetailKf;//库房地址*/
                            _this.formInline.linkMan=data.data.linkMan;//联系人
                            _this.formInline.linkManMobile=data.data.linkManMobile;//联系电话
                            //console.log(data.shipNoticceUserName)
                         }
                         else{
                            _this.common.errorTip(data.errorMessage);
                         }
                    }
                });
            },
            //将填写的本次发货数量加入到对应的行中
            inputCurrQuantity(index,row){
                var _this=this;
                _this.isClick=false;
                if(/^(\-?)(\d+)$/.test(row.sendQuantity)){
                    _this.isCurrNum=true;
                    //验证本次发货数量必须大于发货请求数量
                    if(row.sendQuantity<=_this.createDeliveryData[index].notCurrQuantity){
                        //_this.isTure=true;
                        _this.createDeliveryData[index].currQuantity=row.sendQuantity;
                        //console.log(_this.createDeliveryData[index].noticeUnit)
                    }else{
                        //_this.isTure=false;
                        _this.common.errorTip("本次发货数量必须小于未发数量");
                    }

                }else{
                    //_this.isCurrNum=false;
                    _this.common.errorTip("请填入数字");
                }

            },
            //将填写的送货单位加入到对应的行中
            inputSpeQuantity(index,row){
                var _this=this;
                _this.isClick=false;
                // console.log(row.currQuantity);
                //做参数判断，只允许填入数字
                if(/^(\-?)(\d+)$/.test(row.noticeUnit)){
                    //_this.isNum=true;
                    _this.createDeliveryData[index].noticeUnit=row.noticeUnit;
                    //console.log(_this.createDeliveryData[index].noticeUnit)
                }else{
                   // _this.isNum=false;
                    _this.common.errorTip("请填入数字");
                }
            },
            //去首尾空格
            trimStr(str){
                return str.replace(/(^\s*)|(\s*$)/g,"");
            },
            //处理参数
            dealParms(){
                var _this=this;
                var parms=_this.createDeliveryData;
                var newParms=[];
                for(var i=0;i<parms.length;i++){
                    var parmsObj={};
                    parmsObj.materialCode=parms[i].materialCode;
                    parmsObj.materialName=_this.trimStr(parms[i].materialName);//处理参数，去首尾空格
                    parmsObj.standard=parms[i].standard;
                    parmsObj.materialUnit=parms[i].materialUnit;
                    parmsObj.noticeUnit=parms[i].noticeUnit;
                    parmsObj.shipReqQuantity=parms[i].shipReqQuantity;//发货请求数量
                    parmsObj.currQuantity=parms[i].sendQuantity;//本次发货数量
                    parmsObj.shipRequestNo=parms[i].shipRequestNo;
                    newParms.push(parmsObj);
                }
                return newParms
            },
            //验证本次发货数量
            validSendData(){
                var _this=this;
                var arr=_this.createDeliveryData;
                //console.log(arr)
                for(var i=0;i<arr.length;i++){
                    if(/^(\-?)(\d+)$/.test(arr[i].sendQuantity)){
                        //验证本次发货数量必须大于发货请求数量
                        if(arr[i].sendQuantity<=_this.createDeliveryData[i].notCurrQuantity){
                            if(arr[i].sendQuantity<=0 || !arr[i].sendQuantity){
                                //有一个小于等于0的就返回false
                                console.log(_this.isNum,_this.isCurrNum, _this.isTure,"false处")
                                return false;
                            }
                        }else{
                            _this.isTure=false;
                            _this.common.errorTip("本次发货数量必须小于未发数量");
                            return false;
                        }
                    }else{
                        _this.isCurrNum=false;
                        _this.common.errorTip("请填入数字");
                        return false;
                    }

                }
                //验证都通过的返回true
                console.log(_this.isNum,_this.isCurrNum, _this.isTure,"true前")
                _this.isTure=true;
                _this.isCurrNum=true;
                console.log(_this.isNum,_this.isCurrNum, _this.isTure,"ture后")
                return true;
            },
            //验证送货单位
            validUnitData(){
                var _this=this;
                var arr=_this.createDeliveryData;
                for(var i=0;i<arr.length;i++){
                    if(/^(\-?)(\d+)$/.test(arr[i].noticeUnit)){
                        if(arr[i].noticeUnit<=0 || !arr[i].noticeUnit){
                            //有一个小于等于0的就返回false
                            return false;
                        }

                    }else{
                        _this.isNum=false;
                        _this.common.errorTip("请填入数字");
                        return false;
                    }

                }
                //验证都通过的返回true
                _this.isNum=true;
                return true;
            },
            //保存
            saveNotice(){
                var _this=this;
                _this.suFlag="btnSave";
                _this.valiSaveUpdate();
                _this.isClick=true;
            },
            //提交
            commitNotice(){
                var _this=this;
                _this.suFlag="btnSubmit";
                _this.valiSaveUpdate();
                _this.isClick=true;
            },
            //保存和提交前的验证
            valiSaveUpdate(){
                var _this=this;
                var validSend=_this.validSendData();//验证本次发货数量
                var validUnit=_this.validUnitData();//验证送货单位
                //验证本次发货数量
                if(validSend){
                    if(_this.isCurrNum){
                        //验证本次发货数量必须大于发货请求数量
                        if(_this.isTure){
                            //验证送货单位
                            if(validUnit){
                                if(_this.isNum){
                                    _this.saveUpdateNotice();
                                    //console.log("验证通过")
                                }else{
                                    _this.common.errorTip("送货单位必须是数字");
                                }
                            }else{
                                _this.common.errorTip("请填入送货单位");
                            }
                        }else{
                            _this.common.errorTip("本次发货数量必须小于未发数量");
                        }

                    }else{
                        _this.common.errorTip("本次发货数量必须是数字");
                    }


                }else{
                    _this.common.errorTip("请填入本次发货数量");
                }
            },
            //预入库单的保存和提交方法
            saveUpdateNotice(){
                var _this=this;
                var materList=_this.dealParms();
                //console.log(_this.isNum,_this.isCurrNum, _this.isTure,"保存处")
                var tmp={
                    suFlag:_this.suFlag,//btnSave代表保存 ; btnSubmit代表提交
                    poCode:_this.formInline.poCode,//po编号
                    shipNoticeNo:_this.formInline.shipNoticeNo,//预入库单号
                    shipNoticceUserName:_this.formInline.shipNoticceUserName,//制单人
                    createTime:_this.formInline.createTime,//制单日期
                    shipRequestStoreNo:_this.formInline.shipRequestStoreNo,//库房编号
                    shipAdressNo:_this.formInline.shipAdressNo,//发货地址编号
                    adressDetail:_this.formInline.adressDetail,//详细地址
                    linkMan:_this.formInline.linkMan,//联系人
                    linkManMobile:_this.formInline.linkManMobile,//联系电话
                    shipNoticeBak:_this.formInline.shipNoticeBak,//备注
                    materList:materList,//备件列表
                }
               //console.log(JSON.stringify(tmp));
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"shipNotice/saveUpdateNotice",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if(data.success){
                            _this.common.successTip("成功");
                            window.location.href="#DeliveryNoticeSheet";
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }

                    }
                });
            },
            //   分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                var _this=this;
                _this.pageSize=val;
                _this.queryNoticeDeliverySheet();

            },
            //点击页码分页查询点击事件
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                var _this=this;
                _this.pageIndex=val;
                _this.queryNoticeDeliverySheet();
            },
            // //成功
            // successTip(p) {     //成功时的提示信息
            //     this.$notify({
            //         title: '成功',
            //         message: p,
            //         type: 'success'
            //     });
            // },
            // //错误
            // errorTip(p) {     //错误提示
            //     this.$notify.error({
            //         title: '错误',
            //         message: p
            //     });
            // }
        }
    }
</script>


